package com.mv.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mv.mapper.UserMapper;
import com.mv.pojo.Card;
import com.mv.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class UserServiceImpl implements UserService{
    @Autowired
    private CardService cardService;
    @Autowired
    private UserService userService;
    @Autowired
    private UserMapper userMapper;
    @Override
    public List<User> getUser(String query,String mode) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        if(mode.equals("name")){
            queryWrapper.like(StringUtils.hasLength(query),"user_name",query);
        }else {
            queryWrapper.like(StringUtils.hasLength(query),"phone",query);
        }
        List<User> users = userMapper.selectList(queryWrapper);
        for (User user : users) {
            Integer day=getDay(user.getBirthday());
            user.setDay(day);
        }
        return users;
    }

    private Integer getDay(Date birthday) {
        Integer day=null;
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar now = Calendar.getInstance();             //用来存今天的日期
        Calendar birth = Calendar.getInstance();           //用来存生日日期
        birth.setTime(birthday);   //存入生日日期
        birth.set(Calendar.YEAR,now.get(Calendar.YEAR));   //将生日年份改为今年
        String json = format.format(birthday).substring(5);
        if(!json.equals("02-29")){
            if(birth.get(Calendar.DAY_OF_YEAR)>now.get(Calendar.DAY_OF_YEAR)){
                day=birth.get(Calendar.DAY_OF_YEAR)-now.get(Calendar.DAY_OF_YEAR);
            }else {
                day=now.getActualMaximum(Calendar.DAY_OF_YEAR)-now.get(Calendar.DAY_OF_YEAR);
                day+=birth.get(Calendar.DAY_OF_YEAR);
            }
        }else {
            if(birth.get(Calendar.YEAR)%4==0&&birth.get(Calendar.YEAR)%100!=0||birth.get(Calendar.YEAR)%400==0){
                if(birth.get(Calendar.DAY_OF_YEAR)>now.get(Calendar.DAY_OF_YEAR)){
                    day=birth.get(Calendar.DAY_OF_YEAR)-now.get(Calendar.DAY_OF_YEAR);
                }
            }
        }
        return day;
    }

    @Transactional
    @Override
    public void addUser(User user) {
        String password =DigestUtils.md5DigestAsHex(user.getPassword().getBytes());
        user.setPassword(password);
        userMapper.insert(user);
    }
    @Transactional
    @Override
    public void deleteUser(Integer id) {
        List<Card> cardList = cardService.getCardList(id,null);
        if(cardList!=null&&cardList.size()!=0){
            for (Card card : cardList) {
                cardService.deleteCardById(card.getId());
            }
        }
        userMapper.deleteById(id);
    }
    @Transactional
    @Override
    public void updateUser(User user) {
        userMapper.updateById(user);
    }
    @Override
    public Integer getCount() {
        return userMapper.selectCount(null);
    }

    @Override
    public List<User> getUserName() {
        List<User> userList= new ArrayList<>();
        List<User> users = userMapper.selectList(null);
        for (User user : users) {
            userList.add(new User().setId(user.getId())
                    .setUserName(user.getUserName()).setFlag(false));
        }
        return userList;
    }
    @Transactional
    @Override
    public void deleteBySelectList(List<User> selectList) {
        for (User user : selectList) {
            Integer userId = user.getId();
            userService.deleteUser(userId);
        }
    }

    @Override
    public Integer getUserId(String userName) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name", userName);
        return userMapper.selectOne(queryWrapper).getId();
    }


}
